<template>
  <el-dialog
    v-el-drag-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="服务标签" prop="serviceName">
      <el-input v-model="dataForm.serviceName" placeholder="服务标签" style="width: 400px"></el-input>
    </el-form-item>
    <el-form-item label="图标" prop="iconUrl">
      <el-upload
        class="avatar-uploader"
        :action="this.$http.adornUrl(`/banner/uploadBanner?token=${this.$cookie.get('token')}`)"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload">
        <img v-if="dataForm.iconUrl" :src="dataForm.iconUrl" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </el-form-item>
    <el-form-item label="排序" prop="seq">
      <el-input type="number" v-model="dataForm.seq" placeholder="排序"  style="width: 400px"></el-input>
    </el-form-item>
    <el-form-item label="服务简述" prop="contentDescribe">
      <el-input type="textarea" v-model="dataForm.contentDescribe" placeholder="服务简述"  style="width: 400px"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          seq: 0,
          iconUrl: '',
          contentDescribe: '',
          serviceName: ''
        },
        dataRule: {
          serviceName: [
            { required: true, message: '服务标签不能为空', trigger: 'blur' }
          ],
        }
      }
    },
    methods: {
      handleAvatarSuccess (res, file) {
        this.dataForm.iconUrl = res.data
      },
      beforeAvatarUpload (file) {
        console.log(file)
        const isJPG = file.type === 'image/jpeg' || 'image/png'
        // const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!')
        }
        // if (!isLt2M) {
        //   this.$message.error('上传头像图片大小不能超过 2MB!');
        // }
        // return isJPG && isLt2M;
        return isJPG
      },
      init (id) {
        this.dataForm.id = 0
        this.dataForm.seq = 0
        this.dataForm.iconUrl = ''
        this.dataForm.contentDescribe = ''
        this.dataForm.serviceName = ''
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/service/getGoodsServiceInfoById/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.serviceName = data.data.serviceName
                this.dataForm.seq = data.data.seq
                if (data.data.iconUrl !== null && data.data.iconUrl !== '') {
                  this.dataForm.iconUrl = 'https://image.yilianbaihui.cn/consumer/' + data.data.iconUrl
                }
                this.dataForm.contentDescribe = data.data.contentDescribe
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        if (this.dataForm.iconUrl !== '') {
          let index = this.dataForm.iconUrl.lastIndexOf('\/')
          this.dataForm.iconUrl  = this.dataForm.iconUrl.substring(index + 1, this.dataForm.iconUrl.length)
          this.dataForm.iconUrl = 'banner/' + this.dataForm.iconUrl
        }
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/service/${!this.dataForm.id ? 'addGoodsService' : 'modifyGoodsServiceDetailsById'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'serviceName': this.dataForm.serviceName,
                'seq': this.dataForm.seq,
                'contentDescribe': this.dataForm.contentDescribe,
                'iconUrl': this.dataForm.iconUrl
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 128px;
    height: 128px;
    line-height: 128px;
    text-align: center;
  }
  .avatar {
    width: 128px;
    height: 128px;
    display: block;
  }
</style>
